package org.farng.mp3.object;

import java.nio.charset.*;

import org.farng.mp3.TagOptionSingleton;

/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2002
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 *
 * @author Eric Farng
 * @version $Revision: 1.5 $
 */
public class ObjectStringSizeTerminated extends AbstractMP3Object {
    /**
     * Creates a new ObjectStringSizeTerminated object.
     *
     * @param identifier DOCUMENT ME!
     */
    public ObjectStringSizeTerminated(String identifier) {
        this.identifier = identifier;
    }

    public ObjectStringSizeTerminated(ObjectStringSizeTerminated object) {
        super(object);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        String str = writeString();
        Charset cset = Charset.forName(TagOptionSingleton.getInstance().getCharset());
        if (str != null) {
        	return cset.encode(str).limit();
//            return str.length();
        } else {
            return 0;
        }
    }

    public boolean equals(Object obj) {
        if (obj instanceof ObjectStringSizeTerminated == false) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param str DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readString(String str, int offset) {
        if (str == null) {
            throw new NullPointerException("String is null");
        }

        if ((offset < 0) || (offset >= str.length())) {
            throw new IndexOutOfBoundsException("Offset to String is out of bounds: offset = " + offset + ", string.length()" + str.length());
        }

        value = str.substring(offset);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        return writeString();
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String writeString() {
        return (String) value;
    }
}